<?php
/**
 * ScreenRepo.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-16 16:39:29
 * @modified    2023-02-16 16:39:29
 *
 */

namespace App\Repositories;

use App\Models\Customer;
use App\Models\Product;
use App\Models\ScreenCustomerTemp;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class ScreenRepo
{
    public static function operation()
    {
        return self::operationData();
    }

    private static function operationData()
    {
        // 基地信息
        $baseInfo = [
            ['name' => '溧阳生产基地', 'lng' => 119.48, 'lat' => 31.42],
            ['name' => '杭州生产基地', 'lng' => 120.21, 'lat' => 30.20],
            ['name' => '宜宾生产基地', 'lng' => 104.62, 'lat' => 28.77],
            ['name' => '香港生产基地', 'lng' => 114.16, 'lat' => 22.77],
            ['name' => '东莞生产基地', 'lng' => 113.75, 'lat' => 23.02],
            ['name' => '德国生产基地', 'lng' => 13.25, 'lat' => 52.30],
            ['name' => '美国生产基地', 'lng' => -77.01, 'lat' => 38.91],
            ['name' => '东南亚生产基地', 'lng' => 105.51, 'lat' => 21.02],
            ['name' => '匈牙利生产基地', 'lng' => 19, 'lat' => 47.5],
        ];
        // 荣誉
        $honors = [
            'inland' => [
                "高新技术企业",
                "中国制冷学会会员",
                "中国建筑学会会员",
                "中国制冷空调工业协会会员",
                '江苏省制冷学会会员',
                '2021年度溧阳市企业创新成长奖',
                '2021年度溧阳市税收贡献奖',
                '杭州市临平区科技企业研发中心',
                '职业健康安全管理体系认证证书',
                '质量管理体系认证证书',
                '环境管理体系认证证书',
                '质量服务信誉AAA级企业',
                '中国重质量守信用单位',
                '中国质量诚信品牌信得过企业',
                '浙江省千年质量计量信得过企业',
                '优秀供应商奖'
            ],
            'foreign' => [
                "CE认证",
                "UL认证"
            ]
        ];
        // 出货量、运行量、厂房量
        $counts = [
            'goods' => '10000+',
            'runtime' => '8000+',
            'factory' => '320'
        ];
        // 行业及服务年限

        $deviceCountsBaseData = ScreenCustomerTemp::query()
            ->select([DB::raw('count(*) as total'), 'customer_short_name'])
            ->groupBy('customer_short_name')
            ->orderByDesc('total')
            ->get()
            ->toArray();
        $deviceCounts = [];
        foreach ($deviceCountsBaseData as $item) {
            $deviceCount = $item['total'];
            $enterprise = $item['customer_short_name'];
            $firstData = ScreenCustomerTemp::query()
                ->where('customer_short_name', '=', $enterprise)
                ->orderBy('product_date')
                ->first();
            $firstProductData = $firstData->product_date ?? '';
            if (!$firstProductData) {
                continue;
            }
            // $serviceLength = abs();
            $now = Carbon::now()->toDateString();
            $serviceLength = Carbon::parse($now)->diffInYears(Carbon::parse($firstProductData), true);
            $deviceCounts[] = [
                'enterprise' => $enterprise,
                'devicesCount' => $deviceCount,
                'serviceLength' => max(1, $serviceLength)
            ];
        }

        /*
        $deviceCounts = [
            ['enterprise' => '宁德时代新能源科技股份有限公司', 'devicesCount' => 10, 'serviceLength' => 10],
            ['enterprise' => '维科电池', 'devicesCount' => 55, 'serviceLength' => 8],
            ['enterprise' => '银隆新能源', 'devicesCount' => 42, 'serviceLength' => 7],
            ['enterprise' => '四川时代', 'devicesCount' => 356, 'serviceLength' => 5],
            ['enterprise' => '时代吉利', 'devicesCount' => 83, 'serviceLength' => 5],
            ['enterprise' => '中创新航科技', 'devicesCount' => 181, 'serviceLength' => 5],
            ['enterprise' => '中创新航材料', 'devicesCount' => 113, 'serviceLength' => 5],
            ['enterprise' => '华鼎国联', 'devicesCount' => 15, 'serviceLength' => 6],
            ['enterprise' => '金堂时代', 'devicesCount' => 3, 'serviceLength' => 5],
        ];
        */

        $customersData = Customer::query()->get();
        // 行业分布
        $industryData = [];
        $yearCustomers = [];
        foreach ($customersData as $item) {
            // 增长
            $itemCreateYear = Carbon::parse($item->contact_at)->year;
            if (isset($yearCustomers[$itemCreateYear])) {
                $currentYearCount = (int)$yearCustomers[$itemCreateYear] + 1;
            } else {
                $currentYearCount = 1;
            }
            $yearCustomers[$itemCreateYear] = $currentYearCount;
            // 行业
            $itemIndustry = $item->industry_name;
            if (!$itemIndustry) {
                continue;
            }
            if (isset($industryData[$itemIndustry])) {
                $currentIndustryCount = (int)$industryData[$itemIndustry] + 1;
            } else {
                $currentIndustryCount = 1;
            }
            $industryData[$itemIndustry] = $currentIndustryCount;
        }

        $outputIndustry = [];
        foreach ($industryData as $key => $item) {
            $outputIndustry[] = [
                'industry' => $key,
                'count' => $item
            ];
        }

        $columns = array_column($outputIndustry, 'count');
        array_multisort($columns, SORT_DESC, $outputIndustry);
        $industries = $outputIndustry;
//        $industries = [
//            ['industry' => '锂电池', 'count' => 25],
//            ['industry' => '制药', 'count' => 12],
//            ['industry' => '光伏', 'count' => 10],
//            ['industry' => '食品', 'count' => 8],
//            ['industry' => '军工', 'count' => 3],
//        ];
        // 增长趋势, 按合作开始时间进行统计
        // 创建时间db里面是时间戳，无法根据年份分组，所以逐个取出做统计

        foreach ($customersData as $item) {

        }
        ksort($yearCustomers);
        $increase = [];
        foreach ($yearCustomers as $year => $customer) {
            $increase[] = [
                'year' => $year,
                'count' => $customer
            ];
        }
        $increase = [
            ['year' => 2018, 'count' => 10],
            ['year' => 2019, 'count' => 20],
            ['year' => 2020, 'count' => 30],
            ['year' => 2021, 'count' => 44],
            ['year' => 2022, 'count' => 65],
            ['year' => 2023, 'count' => 93],
            ['year' => "2024Q1", 'count' => 32],
        ];
        // 服务网点
        $servicePoints = [
            ['name' => '服务网点', 'value' => '10+'],
            ['name' => '技术人员', 'value' => '60+'],
            ['name' => '备件仓', 'value' => '10+'],
            ['name' => '响应时间', 'value' => '7*24']
        ];
        // 设备分布

        $deviceDistributeData = Product::query()
            ->select([DB::raw('count(*) as total'), 'province_name'])
            ->from('product')
            ->where('province_name', '<>', '')
            ->orderByDesc('total')
            ->groupBy('province_name')
            ->get()
            ->toArray();
        $deviceDistribute = [];
        foreach ($deviceDistributeData as $index => $item) {
            $key = $index + 1;
            $deviceDistribute[] = [
                'province' => $item['province_name'],
                'ranking' => $key,
                'count' => $item['total']
            ];
        }

        /*
        $deviceDistribute = [
            ['province' => '福建省', 'ranking' => 1, 'count' => 133],
            ['province' => '浙江省', 'ranking' => 2, 'count' => 127],
            ['province' => '江苏省', 'ranking' => 3, 'count' => 86],
            ['province' => '广东省', 'ranking' => 4, 'count' => 79],
            ['province' => '湖南省', 'ranking' => 5, 'count' => 58],
            ['province' => '河北省', 'ranking' => 6, 'count' => 53],
        ];
        */

        // 雇员
        $employee = [
            [
                'name' => '20年以上',
                'value' => 3,
                'level' => 1,
                'subList' => [
                    ['name' => '职能管理岗', 'value' => 2, 'level' => 2],
                    ['name' => '研发技术岗', 'value' => 1, 'level' => 2],
                ]
            ],
            [
                'name' => '15年以上',
                'value' => 16,
                'level' => 1,
                'subList' => [
                    ['name' => '职能管理岗', 'value' => 12, 'level' => 2],
                    ['name' => '研发技术岗', 'value' => 4, 'level' => 2],
                ]
            ],
            [
                'name' => '10年以上',
                'value' => 39,
                'level' => 1,
                'subList' => [
                    ['name' => '职能管理岗', 'value' => 17, 'level' => 2],
                    ['name' => '技术服务岗', 'value' => 17, 'level' => 2],
                    ['name' => '研发技术岗', 'value' => 5, 'level' => 2],
                ]
            ],
            [
                'name' => '5年以上',
                'value' => 43,
                'level' => 1,
                'subList' => [
                    ['name' => '职能管理岗', 'value' => 12, 'level' => 2],
                    ['name' => '技术服务岗', 'value' => 28, 'level' => 2],
                    ['name' => '研发技术岗', 'value' => 3, 'level' => 2],
                ]
            ],
            [
                'name' => '3年以上',
                'value' => 34,
                'level' => 1,
                'subList' => [
                    ['name' => '职能管理岗', 'value' => 15, 'level' => 2],
                    ['name' => '技术服务岗', 'value' => 13, 'level' => 2],
                    ['name' => '研发技术岗', 'value' => 6, 'level' => 2],
                ]
            ]
        ];
        // 国内基地
        $inlandBase = [
            ['name' => '中国四川宜宾厂区', 'lng' => 104.62, 'lat' => 28.77, 'capacity' => 50],
            // ['name' => '中国广东东莞厂区', 'lng' => 113.75, 'lat' => 23.05, 'capacity' => 120],
            ['name' => '中国浙江杭州厂区', 'lng' => 120.15, 'lat' => 30.28, 'capacity' => 90],
            ['name' => '中国江苏溧阳厂区', 'lng' => 119.48, 'lat' => 31.42, 'capacity' => 70]
        ];
        // 国外基地
        $foreignBase = [
            ['name' => '匈牙利布达佩斯基地', 'lng' => 19.15, 'lat' => 47.26, 'capacity' => 0]
        ];

        return [
            'base_info' => $baseInfo,
            'honors' => $honors,
            'counts' => $counts,
            'device_counts' => $deviceCounts,
            'industries' => $industries,
            'increase' => $increase,
            'service_points' => $servicePoints,
            'device_distribute' => $deviceDistribute,
            'employee' => $employee,
            'inland_base' => $inlandBase,
            'foreign_base' => $foreignBase
        ];
    }
}